class ClosureDelegate {
    def request = [:]


}

class Controller {
    def aMemberInController = "x"

    //This closure can refer to closureDelegate's members, but 
    //those variables are not available in Controller methods 
    //called by this closure.
    def aClosure={
       aMethodInController(request.inClosureDelegate)
       request.fromController = aMemberInController
   }

    def aMethodInController(str){
        aMemberInController = aMemberInController + str
        //Would cause groovy.lang.MissingPropertyException: No such property: request for class: Controller
       // println request

    }

   //Doesn't work yet!!
   def aMethodUsedAsAClosure(){
      println request
   }

}

def closureDelegate = new ClosureDelegate()
closureDelegate.request.inClosureDelegate = "hi"
def controller = new Controller()
//Here's the trick.  Now a closue can refer to closureDelegate's members
controller.aClosure.delegate =  closureDelegate
controller.aClosure()
println closureDelegate.request



//Doesn't work yet-- trying to treat the method as a closure and have closureDelegate's request member be availalbe to it
controller.&aMethodUsedAsAClosure.delegate = closureDelegate
//controller.&aMethodUsedAsAClosure.request
println controller.&aMethodUsedAsAClosure.delegate

def cl = controller.&aMethodUsedAsAClosure
cl.delegate = closureDelegate
println cl.delegate
println controller.aClosure.delegate
//cl()


